package com.school.graduation.goods.presenter;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.school.graduation.R;
import com.school.graduation.bean.GoodsBean;
import com.school.graduation.detailed.view.DetailedActivityNew;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class RecommendRecyclerAdapterNew extends RecyclerView.Adapter<RecommendViewHolder> {

    private Context context;
    private List<GoodsBean> goodsBeens;
    private TextView loadingText;

    public RecommendRecyclerAdapterNew(List<GoodsBean> goodsBeens) {
        this.goodsBeens = goodsBeens;
    }

    @Override
    public int getItemViewType(int position) {
        if (position==goodsBeens.size()) return 1;
        return 0;
    }

    @Override
    public RecommendViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        switch (viewType){
            case 0:
                return new RecommendViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recommend_new,parent,false));
            case 1:
                return new RecommendViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_load_more,parent,false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecommendViewHolder holder, final int position) {
        if (position==goodsBeens.size()){
            loadingText = holder.getView(R.id.loading_text);
            loadingText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    loadingText.setText("正在加载...");
                }
            });
            if (goodsBeens.size() ==1) loadingText.setVisibility(View.GONE);
        }else {
            CardView item = holder.getView(R.id.recommend_item);
            CircleImageView head = holder.getView(R.id.good_item_head);
            ImageView img = holder.getView(R.id.good_item_img);
            TextView name = holder.getView(R.id.good_item_name);
            TextView price = holder.getView(R.id.good_item_money);
            final TextView content = holder.getView(R.id.good_item_detail);
            TextView time = holder.getView(R.id.good_item_time);

            GoodsBean bean = goodsBeens.get(position);
            Glide.with(context).load(bean.getImg()).into(img);
            name.setText(bean.getSellerName());
            price.setText(bean.getPrice()+"");
            content.setText(bean.getDetail());
            String timeText = bean.getMeta().getCreateAt().substring(0,10)+" "+bean.getMeta().getCreateAt().substring(11,16);
            time.setText(timeText);

            item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickRecommendItem(position);
                }
            });
        }
    }

    private void clickRecommendItem(int position) {
        Bundle bundle = new Bundle();
        bundle.putString("item",goodsBeens.get(position).get_id());
        DetailedActivityNew.jumpToDetail(context,bundle);
    }

    @Override
    public int getItemCount() {
        return goodsBeens.size();
    }
}
